// SKNotes - a note taking and gathering tool
// Copyright (C) 2009  Stefan Kueng
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

namespace SKNotes.Configuration
{
    using System;
    using System.Configuration;

	public abstract class ValueSettingBase : Setting
	{

		protected ValueSettingBase( string name ) :
			this( name, null )
		{
		}

		protected ValueSettingBase( string name, object defaultValue )
		{
			if ( string.IsNullOrEmpty( name ) )
			{
				throw new ArgumentNullException( "name" );
			}

			this.name = name;
			DefaultValue = defaultValue;
			SerializeAs = SettingsSerializeAs.String;
		} 

		public string Name
		{
			get { return this.name; }
		}

		public object DefaultValue { get; set; }

		public SettingsSerializeAs SerializeAs { get; set; }

		public bool LoadUndefinedValue { get; set; }

		public bool SaveUndefinedValue { get; set; }

		public bool HasValue
		{
			get { return Value != null; }
		} 

		public override bool HasChanged
		{
			get
			{
				object originalValue = OriginalValue;
				object value = Value;

				if ( originalValue == value )
				{
					return false;
				}

				if ( originalValue != null && originalValue.Equals( value ) )
				{
					return false;
				}

				return true;
			}
		}

		public abstract object OriginalValue
		{
			get;
		}

		public abstract object Value
		{
			get;
			set;
		}

		public override string ToString()
		{
			return string.Concat( name, "=", Value );
		} 

		private readonly string name;

	}
}
